﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using System;

namespace ALEXBlog.Common.JsonHelper
{
    public class Appsettings
    {
        static IConfiguration Configuration;

        static Appsettings()
        {
            Configuration = new ConfigurationBuilder()
                .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
                .Build();
        }

        public Appsettings(string path)
        {
            Configuration = new ConfigurationBuilder()
                    .Add(new JsonConfigurationSource { Path = path, ReloadOnChange = true })
                    .Build();
        }

        /// <summary>
        /// 封装要操作的字符
        /// </summary>
        /// <param name="sections"></param>
        /// <returns></returns>
        public static string GetAppsettingsValue(params string[] sections)
        {
            try
            {
                var val = string.Empty;
                for (int i = 0; i < sections.Length; i++)
                {
                    val += sections[i] + ":";
                }

                return Configuration[val.TrimEnd(':')];
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }

        }


    }
}
